package com.amazonservices.mws.products.client;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.mws.MarketplaceWebServiceClient;
import com.amazonaws.mws.MarketplaceWebServiceConfig;
import com.amazonaws.mws.MarketplaceWebServiceException;
import com.amazonaws.mws.model.GetReportListRequest;
import com.amazonaws.mws.model.GetReportListResponse;
import com.amazonaws.mws.model.GetReportRequest;
import com.amazonaws.mws.model.GetReportResponse;
import com.amazonaws.mws.model.GetReportResult;
import com.amazonaws.mws.model.IdList;
import com.amazonaws.mws.model.ReportRequestInfo;
import com.amazonaws.mws.model.RequestReportRequest;
import com.amazonaws.mws.model.RequestReportResponse;
import com.amazonaws.mws.model.RequestReportResult;

public class SellingProduction
{

	private static String accessKeyId = "AKIAJ5OZAQSWFXMKBVSA";
	
	private static String secretAccessKey  = "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";
	
	private static String serviceUrl = "https://mws.amazonservices.com";
	
	private static String applicationName = "ApplicationName";
	
	private static String applicationVersion  = "1.0";
	
	private static String merchantId = "AVFQBF6C564NE";
	
	private static String marketplaceId ="ATVPDKIKX0DER";
	
	public static void main(String[] args)
	{
		MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
		config.setServiceURL(serviceUrl);
		
		MarketplaceWebServiceClient service = new MarketplaceWebServiceClient(accessKeyId,
																			  secretAccessKey,
																			  applicationName,
																			  applicationVersion,
																			  config
																				);
		
		List<String> ids = new ArrayList<String>();
		ids.add(marketplaceId);
		
		IdList marketplaceIdList = new IdList();
		marketplaceIdList.setId(ids);
		
		RequestReportRequest  reportRequestRequest = new RequestReportRequest ();
		reportRequestRequest.setMerchant(merchantId);
		reportRequestRequest.setMarketplaceIdList(marketplaceIdList);		
		reportRequestRequest.setReportType("_GET_MERCHANT_LISTINGS_DATA_LITE_");

		//reportRequestRequest.setStartDate(value);
		//reportRequestRequest.setEndDate(value);
		
		try
		{
//			RequestReportResponse requestResponse = service.requestReport(reportRequestRequest);
//			
//			Thread.sleep(15000);
//			
//			RequestReportResult requestReportResult = requestResponse.getRequestReportResult();
//			
//			
//			ReportRequestInfo reportRequestInfo = requestReportResult.getReportRequestInfo();
//			
//			//String reportId = reportRequestInfo.getGeneratedReportId();
//			
////			String status = reportRequestInfo.getReportProcessingStatus();
////			
////			if (!"_DONE_".equalsIgnoreCase(status))
////			{
////				System.out.println("report not generated");
////				return;
////			}
//			
//			//如果没有返回reportid,那么就采用其他方式处理
//			if(!reportRequestInfo.isSetGeneratedReportId())
//			{
//				GetReportListRequest getReportListRequest = new GetReportListRequest(); 
//				
//				getReportListRequest.setMerchant(merchantId);
//				
//				GetReportListResponse getReportListResponse  = service.getReportList(getReportListRequest);
//				
//				System.out.println(getReportListResponse.toXML());
//				
//				return;
//			}
			String reportId = "1863186774016935";
			
			GetReportRequest getReportRequest = new GetReportRequest();
			getReportRequest.setMerchant(merchantId);
			getReportRequest.setReportId(reportId);			
			
			String fileName = "d:\\r.txt";
			
			OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
			
			getReportRequest.setReportOutputStream(out);
			
			GetReportResponse getReportResponse = service.getReport(getReportRequest);
			
			getReportRequest.getReportOutputStream().close();
			
			GetReportResult getReportResult = getReportResponse.getGetReportResult();
			
			System.out.println(getReportResult.getMD5Checksum());
			
			
		} catch (MarketplaceWebServiceException e)
		{
			e.printStackTrace();
		} 
//		catch (InterruptedException e)
//		{
//			e.printStackTrace();
//		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		
		
		
		
	}

}
